import "./reset.css";
import dayjs from "dayjs";
import roleForm from "../form/role.vue";
import editForm from "../form/index.vue";
import { zxcvbn } from "@zxcvbn-ts/core";
import { handleTree } from "@/utils/tree";
import { message } from "@/utils/message";
import userAvatar from "@/assets/user.jpg";
import { usePublicHooks } from "../../hooks";
import { addDialog } from "@/components/ReDialog";
import type { PaginationProps } from "@pureadmin/table";
import type { FormItemProps, RoleFormItemProps } from "../utils/types";
import * as XLSX from "xlsx";
import {
  // getKeyList,
  isAllEmpty,
  hideTextAtIndex,
  deviceDetection
} from "@pureadmin/utils";
import {
  getRoleIds,
  getDeptList,
  getUserList,
  getAllRoleList,
  createUser,
  updateUser,
  deleteUser,
  updatePassword,
  updateUserRole,
  batchDeleteUser
} from "@/api/system";
import {
  ElForm,
  ElInput,
  ElFormItem,
  ElProgress,
  ElMessageBox
} from "element-plus";
import {
  type Ref,
  h,
  ref,
  toRaw,
  watch,
  computed,
  reactive,
  onMounted
} from "vue";
import { getAvatar } from "@/api/user";

export function useUser(tableRef: Ref, treeRef: Ref) {
  const form = reactive({
    // 左侧部门树的id
    deptId: "",
    userInfo: "",
    status: ""
  });
  const formRef = ref();
  const ruleFormRef = ref();
  const dataList = ref([]);
  const loading = ref(true);
  const switchLoadMap = ref({});
  const { switchStyle } = usePublicHooks();
  const higherDeptOptions = ref();
  const treeData = ref([]);
  const treeLoading = ref(true);
  const selectedNum = ref(0);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });

  // 添加头像缓存
  const avatarCache = ref<Record<string, string>>({});

  const columns: TableColumnList = [
    {
      label: "勾选列", // 如果需要表格多选，此处label必须设置
      type: "selection",
      fixed: "left",
      reserveSelection: true // 数据刷新后保留选项
    },
    {
      label: "用户编号",
      prop: "ID",
      width: 90
    },
    {
      label: "用户头像",
      prop: "avatar",
      cellRenderer: ({ row }) => {
        const avatarUrl =
          avatarCache.value[row.avatar] || row.avatar || userAvatar;

        // 如果头像未缓存且存在，则异步加载
        if (row.avatar && !avatarCache.value[row.avatar]) {
          getUserAvatar(row.avatar);
        }

        return (
          <el-image
            fit="cover"
            preview-teleported={true}
            src={avatarUrl}
            preview-src-list={Array.of(avatarUrl)}
            class="w-[24px] h-[24px] rounded-full align-middle"
          />
        );
      },
      width: 90
    },
    {
      label: "用户名称",
      prop: "username",
      minWidth: 130
    },
    {
      label: "用户昵称",
      prop: "nickname",
      minWidth: 130
    },
    {
      label: "性别",
      prop: "sex",
      minWidth: 90,
      cellRenderer: ({ row, props }) => (
        <el-tag
          size={props.size}
          type={row.sex === 1 ? "danger" : null}
          effect="plain"
        >
          {row.sex === 1 ? "女" : "男"}
        </el-tag>
      )
    },
    {
      label: "部门",
      prop: "department.name",
      minWidth: 90
    },
    {
      label: "手机号码",
      prop: "phone",
      minWidth: 90,
      formatter: ({ phone }) => hideTextAtIndex(phone, { start: 3, end: 6 })
    },
    {
      label: "状态",
      prop: "status",
      minWidth: 90,
      cellRenderer: scope => (
        <el-switch
          size={scope.props.size === "small" ? "small" : "default"}
          loading={switchLoadMap.value[scope.index]?.loading}
          v-model={scope.row.status}
          active-value={1}
          inactive-value={0}
          active-text="已启用"
          inactive-text="已停用"
          inline-prompt
          style={switchStyle.value}
          onChange={() => onChange(scope as any)}
        />
      )
    },
    {
      label: "创建时间",
      minWidth: 90,
      prop: "createTime",
      formatter: ({ createTime }) =>
        dayjs(createTime).format("YYYY-MM-DD HH:mm:ss")
    },
    {
      label: "操作",
      fixed: "right",
      width: 180,
      slot: "operation"
    }
  ];
  const buttonClass = computed(() => {
    return [
      "h-[20px]!",
      "reset-margin",
      "text-gray-500!",
      "dark:text-white!",
      "dark:hover:text-primary!"
    ];
  });
  // 重置的新密码
  const pwdForm = reactive({
    newPwd: ""
  });
  const pwdProgress = [
    { color: "#e74242", text: "非常弱" },
    { color: "#EFBD47", text: "弱" },
    { color: "#ffa500", text: "一般" },
    { color: "#1bbf1b", text: "强" },
    { color: "#008000", text: "非常强" }
  ];
  // 当前密码强度（0-4）
  const curScore = ref();
  const roleOptions = ref([]);

  function onChange({ row, index }) {
    ElMessageBox.confirm(
      `确认要<strong>${
        row.status === 0 ? "停用" : "启用"
      }</strong><strong style='color:var(--el-color-primary)'>${
        row.username
      }</strong>用户吗?`,
      "系统提示",
      {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        dangerouslyUseHTMLString: true,
        draggable: true
      }
    )
      .then(async () => {
        switchLoadMap.value[index] = Object.assign(
          {},
          switchLoadMap.value[index],
          {
            loading: true
          }
        );
        // 调用修改用户状态接口
        try {
          // 调用启用/停用角色接口
          const newStatus = row.status;
          const response = await updateUser({
            id: row.ID,
            status: newStatus,
            username: row.username,
            nickname: row.nickname,
            phone: row.phone,
            email: row.email,
            sex: row.sex,
            deptId: row.deptId
          });

          // 请求成功处理
          if (response.success) {
            // 更新本地数据
            row.status = newStatus;
            message(
              `已${newStatus === 1 ? "启用" : "停用"}${row.username}用户`,
              {
                type: "success"
              }
            );
          } else {
            message(`操作失败: ${response.msg || "未知错误"}`, {
              type: "error"
            });
            row.status = !newStatus; // 还原状态
          }
        } catch {
          // 请求失败处理
          row.status = row.status === 0 ? 1 : 0; // 状态还原
        } finally {
          // 无论成功失败，都关闭加载状态
          switchLoadMap.value[index] = Object.assign(
            {},
            switchLoadMap.value[index],
            {
              loading: false
            }
          );
        }
      })
      .catch(() => {
        row.status === 0 ? (row.status = 1) : (row.status = 0);
      });
  }
  function handleDelete(row) {
    deleteUser({
      ID: row.ID
    }).then(res => {
      if (res.success) {
        message(`您删除了用户编号为${row.ID}的这条数据`, { type: "success" });
        onSearch();
      } else {
        message(res.msg, {
          type: "error"
        });
      }
    });
  }

  function handleSizeChange(val: number) {
    pagination.pageSize = val;
    onSearch();
  }
  // 修改getUserAvatar方法，添加缓存机制
  async function getUserAvatar(avatar: string): Promise<string> {
    // 检查缓存
    if (avatar && avatarCache.value[avatar]) {
      return avatarCache.value[avatar];
    }

    if (avatar) {
      try {
        const res = await getAvatar(avatar);
        const url = res ? URL.createObjectURL(res) : userAvatar;
        // 更新缓存
        avatarCache.value[avatar] = url;
        return url;
      } catch {
        return userAvatar;
      }
    }
    return userAvatar;
  }
  function handleCurrentChange(val: number) {
    pagination.currentPage = val;
    onSearch();
  }

  /** 当CheckBox选择项发生变化时会触发该事件 */
  function handleSelectionChange(val) {
    selectedNum.value = val.length;
    // 重置表格高度
    tableRef.value.setAdaptive();
  }

  /** 取消选择 */
  function onSelectionCancel() {
    selectedNum.value = 0;
    // 用于多选表格，清空用户的选择
    tableRef.value.getTableRef().clearSelection();
  }

  /** 批量删除 */
  function onbatchDel() {
    // 返回当前选中的行
    const curSelected = tableRef.value.getTableRef().getSelectionRows();
    const userIds = curSelected.map(item => item.ID);
    batchDeleteUser({
      ids: userIds
    }).then(res => {
      if (res.success) {
        message(res.msg, { type: "success" });
        tableRef.value.getTableRef().clearSelection();
        onSearch();
      } else {
        message(res.msg, {
          type: "error"
        });
      }
    });
  }

  async function onSearch() {
    loading.value = true;
    // 传递分页信息给后端
    const { data } = await getUserList({
      ...toRaw(form),
      currentPage: pagination.currentPage,
      pageSize: pagination.pageSize
    });
    dataList.value = data.list;
    pagination.total = data.total;
    // 不再从后端获取分页信息

    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    form.deptId = "";
    treeRef.value.onTreeReset();
    onSearch();
  };

  function onTreeSelect({ ID, selected }) {
    form.deptId = selected ? String(ID) : "";
    onSearch();
  }

  function formatHigherDeptOptions(treeList) {
    // 根据返回数据的status字段值判断追加是否禁用disabled字段，返回处理后的树结构，用于上级部门级联选择器的展示（实际开发中也是如此，不可能前端需要的每个字段后端都会返回，这时需要前端自行根据后端返回的某些字段做逻辑处理）
    if (!treeList || !treeList.length) return;
    const newTreeList = [];
    for (let i = 0; i < treeList.length; i++) {
      treeList[i].disabled = treeList[i].status === 0 ? true : false;
      formatHigherDeptOptions(treeList[i].children);
      newTreeList.push(treeList[i]);
    }
    return newTreeList;
  }

  function openDialog(title = "新增", row?: FormItemProps) {
    addDialog({
      title: `${title}用户`,
      props: {
        formInline: {
          title,
          higherDeptOptions: formatHigherDeptOptions(higherDeptOptions.value),
          deptId: row?.department?.ID ?? 0,
          nickname: row?.nickname ?? "",
          username: row?.username ?? "",
          password: row?.password ?? "",
          phone: row?.phone ?? "",
          email: row?.email ?? "",
          sex: row?.sex ?? 1,
          status: row?.status ?? 1
        }
      },
      width: "46%",
      draggable: true,
      fullscreen: deviceDetection(),
      fullscreenIcon: true,
      closeOnClickModal: false,
      contentRenderer: () => h(editForm, { ref: formRef, formInline: null }),
      beforeSure: (done, { options }) => {
        const FormRef = formRef.value.getRef();
        const curData = options.props.formInline as FormItemProps;
        function chores() {
          message(`您${title}了用户名称为${curData.username}的这条数据`, {
            type: "success"
          });
          done(); // 关闭弹框
          onSearch(); // 刷新表格数据
        }
        FormRef.validate(valid => {
          if (valid) {
            // 表单规则校验通过
            if (title === "新增") {
              // 实际开发先调用新增接口，再进行下面操作
              createUser(curData).then(res => {
                if (res.success) {
                  chores();
                } else {
                  message(res.msg, {
                    type: "error"
                  });
                }
              });
            } else {
              // 实际开发先调用修改接口，再进行下面操作
              curData.ID = row.ID;
              updateUser(curData).then(res => {
                if (res.success) {
                  chores();
                } else {
                  message(res.msg, {
                    type: "error"
                  });
                }
              });
            }
          }
        });
      }
    });
  }

  watch(
    pwdForm,
    ({ newPwd }) =>
      (curScore.value = isAllEmpty(newPwd) ? -1 : zxcvbn(newPwd).score)
  );

  /** 重置密码 */
  function handleReset(row) {
    addDialog({
      title: `重置 ${row.username} 用户的密码`,
      width: "30%",
      draggable: true,
      closeOnClickModal: false,
      fullscreen: deviceDetection(),
      contentRenderer: () => (
        <>
          <ElForm ref={ruleFormRef} model={pwdForm}>
            <ElFormItem
              prop="newPwd"
              rules={[
                {
                  required: true,
                  message: "请输入新密码",
                  trigger: "blur"
                }
              ]}
            >
              <ElInput
                clearable
                show-password
                type="password"
                v-model={pwdForm.newPwd}
                placeholder="请输入新密码"
              />
            </ElFormItem>
          </ElForm>
          <div class="my-4 flex">
            {pwdProgress.map(({ color, text }, idx) => (
              <div
                class="w-[19vw]"
                style={{ marginLeft: idx !== 0 ? "4px" : 0 }}
              >
                <ElProgress
                  striped
                  striped-flow
                  duration={curScore.value === idx ? 6 : 0}
                  percentage={curScore.value >= idx ? 100 : 0}
                  color={color}
                  stroke-width={10}
                  show-text={false}
                />
                <p
                  class="text-center"
                  style={{ color: curScore.value === idx ? color : "" }}
                >
                  {text}
                </p>
              </div>
            ))}
          </div>
        </>
      ),
      closeCallBack: () => (pwdForm.newPwd = ""),
      beforeSure: done => {
        ruleFormRef.value.validate(valid => {
          if (valid) {
            // 表单规则校验通过
            // 根据实际业务使用pwdForm.newPwd和row里的某些字段去调用重置用户密码接口即可
            updatePassword({
              id: row.ID,
              newPassword: pwdForm.newPwd
            }).then(res => {
              if (res.success) {
                message("修改密码成功", {
                  type: "success"
                });
                done(); // 关闭弹框
                onSearch(); // 刷新表格数据
              } else {
                message(res.msg, {
                  type: "error"
                });
              }
            });
          }
        });
      }
    });
  }

  /** 分配角色 */
  async function handleRole(row) {
    // 选中的角色列表
    const initialIds = (await getRoleIds({ userId: row.ID })).data ?? [];
    // 深拷贝初始角色列表，用于后续比较
    const originalIds = [...initialIds].sort();
    addDialog({
      title: `分配 ${row.username} 用户的角色`,
      props: {
        formInline: {
          username: row?.username ?? "",
          nickname: row?.nickname ?? "",
          roleOptions: roleOptions.value ?? [],
          ids: initialIds
        }
      },
      width: "400px",
      draggable: true,
      fullscreen: deviceDetection(),
      fullscreenIcon: true,
      closeOnClickModal: false,
      contentRenderer: () => h(roleForm),
      beforeSure: (done, { options }) => {
        const curData = options.props.formInline as RoleFormItemProps;
        // 对当前选择的角色ID进行排序，以便比较
        const currentIds = [...curData.ids].sort();
        // 比较角色是否有变化
        if (
          originalIds.length === currentIds.length &&
          originalIds.every((id, index) => id === currentIds[index])
        ) {
          message("角色没有变化，无需提交", {
            type: "warning"
          });
          return;
        }
        updateUserRole({
          id: row.ID,
          roleIds: curData.ids
        }).then(res => {
          if (res.success) {
            message("分配角色成功", {
              type: "success"
            });
            done(); // 关闭弹框
          } else {
            message(res.msg, {
              type: "error"
            });
          }
        });
      }
    });
  }
  /** 导出表格数据 */
  function exportTable() {
    if (dataList.value.length === 0) {
      message("当前没有数据可导出", { type: "warning" });
      return;
    }

    loading.value = true;

    // 处理导出数据
    const exportData = dataList.value.map(row => ({
      用户编号: row.ID,
      用户名称: row.username,
      用户昵称: row.nickname,
      性别: row.sex === 1 ? "女" : "男",
      年龄: row.age || "",
      邮箱: row.email || "",
      部门: row.department?.name || "",
      手机号码: row.phone || "",
      状态: row.status === 1 ? "已启用" : "已停用",
      创建时间: dayjs(row.createTime).format("YYYY-MM-DD HH:mm:ss")
    }));

    // 创建工作表和工作簿
    const worksheet = XLSX.utils.json_to_sheet(exportData);
    const workbook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(workbook, worksheet, "用户数据");

    // 导出文件
    XLSX.writeFile(
      workbook,
      `用户数据_${dayjs().format("YYYYMMDDHHmmss")}.xlsx`
    );

    loading.value = false;
    message("导出成功", { type: "success" });
  }
  onMounted(async () => {
    treeLoading.value = true;
    onSearch();

    // 归属部门
    const { data } = await getDeptList();
    higherDeptOptions.value = handleTree(data);
    treeData.value = handleTree(data);
    treeLoading.value = false;

    // 角色列表
    roleOptions.value = (await getAllRoleList()).data;
  });
  return {
    form,
    loading,
    columns,
    dataList,
    treeData,
    treeLoading,
    selectedNum,
    pagination,
    buttonClass,
    deviceDetection,
    onSearch,
    resetForm,
    onbatchDel,
    openDialog,
    onTreeSelect,
    handleDelete,
    handleReset,
    handleRole,
    handleSizeChange,
    onSelectionCancel,
    handleCurrentChange,
    handleSelectionChange,
    exportTable
  };
}
